local hx__rende_chen = fk.CreateSkill {
  name = "hx__rende_chen",
  tags = {  },
}

hx__rende_chen:addEffect("active", {
  name = "hx__rende_chen",
  prompt = "#hx__rende_chen-promot",
  interaction = function(self, player)
    local choices = { }
    if player:getMark("hx__rende_chen-phase") == 0 then
      table.insert(choices, "hx__rende_chen")
    end
    if player:getMark("@hx__rende_chen") > 0 then
      table.insertTable(choices, player:getViewAsCardNames(hx__rende_chen.name, {"slash"}))
    end
    if player:getMark("@hx__rende_chen") > 1 then
      table.insertTable(choices, player:getViewAsCardNames(hx__rende_chen.name, {"peach"}))
    end
    return UI.ComboBox { choices = choices }
  end,
  card_filter = function(self, player, to_select, selected)
    return self.interaction.data == "hx__rende_chen"
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    if self.interaction.data == "hx__rende_chen" then
      return #selected == 0 and to_select ~= player
    elseif self.interaction.data ~= nil then
      local to_use = Fk:cloneCard(self.interaction.data)
      to_use.skillName = hx__rende_chen.name
      if (#selected == 0 or to_use.multiple_targets) and player:isProhibited(to_select, to_use) then
        return false
      end
      return to_use.skill:targetFilter(player, to_select, selected, selected_cards, to_use)
    end
  end,
  feasible = function(self, player, selected, selected_cards)
    if self.interaction.data == "hx__rende_chen" then
      return #selected_cards > 0 and #selected == 1
    elseif self.interaction.data ~= nil then
      local to_use = Fk:cloneCard(self.interaction.data)
      to_use.skillName = hx__rende_chen.name
      return to_use.skill:feasible(player, selected, selected_cards, to_use)
    end
  end,
  on_use = function(self, room, effect)
    ---@type string
    local skillName = hx__rende_chen.name
    local player = effect.from
    if self.interaction.data == "hx__rende_chen" then
      local target = effect.tos[1]
      room:moveCardTo(effect.cards, Player.Hand, target, fk.ReasonGive, skillName, nil, false, player)
      player:drawCards(1, hx__rende_chen.name)
      room:setPlayerMark(player, "hx__rende_chen-phase", 1)
      if player:getMark("@hx__rende_chen") < 2 then
        room:addPlayerMark(player, "@hx__rende_chen", 1)
      end
    else
      if self.interaction.data == "slash" then
        room:removePlayerMark(player, "@hx__rende_chen", 1)
      elseif self.interaction.data == "peach" then
        room:removePlayerMark(player, "@hx__rende_chen", 2)
      end
      local use = {
        from = player,
        tos = effect.tos,
        card = Fk:cloneCard(self.interaction.data),
      }
      use.card.skillName = skillName
      room:useCard(use)
    end
  end,
})

local hx__rende_chenTriggerViewAsCanTrigger = function(self, event, target, player, data)
  return target == player and player:hasSkill(hx__rende_chen.name) and (player:getMark("@hx__rende_chen") > 0 and
    data.pattern and Exppattern:Parse(data.pattern):matchExp("slash")) or (player:getMark("@hx__rende_chen") > 1 and
    data.pattern and Exppattern:Parse(data.pattern):matchExp("peach"))
end

local hx__rende_chenTriggerViewAsOnCost = function(self, event, target, player, data)
  local basicCards = {"slash","peach"}
  local names = {}
  for _, name in ipairs(basicCards) do
    local card = Fk:cloneCard(name)
    if Exppattern:Parse(data.pattern):match(card) then
      table.insertIfNeed(names, name)
    end
  end
  if #names > 0 then
    local name = names[1]
    if #names > 1 then
      name = table.every(names, function(str) return string.sub(str, -5) == "slash" end) and "slash" or "peach"
    end
    if player.room:askToSkillInvoke(player, { skill_name = hx__rende_chen.name, prompt = "#hx__rende_chen-invoke:::" .. name }) then
      event:setCostData(self, names)
      return true
    end
  end
end

hx__rende_chen:addEffect(fk.AskForCardUse, {
  mute = true,
  can_trigger = hx__rende_chenTriggerViewAsCanTrigger,
  on_cost = hx__rende_chenTriggerViewAsOnCost,
  on_use = function(self, event, target, player, data)
    ---@type string
    local skillName = hx__rende_chen.name
    local room = player.room
    local names = event:getCostData(self)
    local extra_data = data.extraData
    local isAvailableTarget = function(card, p)
      if extra_data then
        if type(extra_data.must_targets) == "table" and #extra_data.must_targets > 0 and
            not table.contains(extra_data.must_targets, p.id) then
          return false
        end
        if type(extra_data.exclusive_targets) == "table" and #extra_data.exclusive_targets > 0 and
            not table.contains(extra_data.exclusive_targets, p.id) then
          return false
        end
      end
      return not player:isProhibited(p, card) and card.skill:modTargetFilter(player, p, {}, card, extra_data)
    end
    local findCardTarget = function(card)
      local tos = {}
      for _, p in ipairs(room.alive_players) do
        if isAvailableTarget(card, p) then
          table.insert(tos, p)
        end
      end
      return tos
    end
    names = table.filter(names, function (c_name)
      local card = Fk:cloneCard(c_name)
      return not player:prohibitUse(card) and (card.skill:getMinTargetNum(player) == 0 or #findCardTarget(card) > 0)
    end)
    if #names == 0 then return false end
    local name = room:askToChoice(player, { choices = names, skill_name = skillName, prompt = "#hx__rende_chen-name" })
    player:broadcastSkillInvoke(skillName)
    if name == "slash" then
      room:removePlayerMark(player, "@hx__rende_chen", 1)
    elseif name == "peach" then
      room:removePlayerMark(player, "@hx__rende_chen", 2)
    end
    local card = Fk:cloneCard(name)
    card.skillName = skillName
    data.result = {
      from = player,
      card = card,
    }
    if card.skill:getMinTargetNum(player) == 1 then
      local tos = findCardTarget(card)
      if #tos == 1 then
        data.result.tos = tos
      elseif #tos > 1 then
        data.result.tos = room:askToChoosePlayers(
          player,
          {
            targets = tos,
            min_num = 1,
            max_num = 1,
            prompt = "#hx__rende_chen-target:::" .. name,
            skill_name = skillName,
            cancelable = false,
            no_indicate = true
          }
        )
      else
        return false
      end
    end
    if data.eventData then
      data.result.toCard = data.eventData.toCard
      data.result.responseToEvent = data.eventData.responseToEvent
    end
    return true
  end
})


hx__rende_chen:addLoseEffect(function (self, player)
  if player:getMark("@hx__rende_chen") ~= 0 then
    player.room:setPlayerMark(player, "@hx__rende_chen", 0)
  end
end)


return hx__rende_chen